package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.OrderSendRecord;
import com.ruoyi.system.service.IOrderSendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlarmHandleRecordMapper;
import com.ruoyi.system.domain.AlarmHandleRecord;
import com.ruoyi.system.service.IAlarmHandleRecordService;

/**
 * 报警记录Service业务层处理
 * 
 * @author wp
 * @date 2023-08-10
 */
@Service
public class AlarmHandleRecordServiceImpl extends ServiceImpl<AlarmHandleRecordMapper, AlarmHandleRecord> implements IAlarmHandleRecordService {

    @Autowired
    private AlarmHandleRecordMapper alarmHandleRecordMapper;
    @Autowired
    private IOrderSendRecordService orderSendRecordService;

    @DataScope
    @Override
    public List<AlarmHandleRecord> selectList(AlarmHandleRecord alarmHandleRecord) {
        return alarmHandleRecordMapper.list(alarmHandleRecord);
    }

    /**
     * 获取报警处理记录详情
     * @return
     */
    @Override
    public JSONObject getDetail(Long orderSendRecordId){
        //查询列表数据
        OrderSendRecord record = orderSendRecordService.getById(orderSendRecordId);
        if (record == null) throw new ServiceException("该指令记录不存在");
        JSONObject content = record.getContent();
        String urgency = "true".equals(content.getString("urgency")) ? "是" : "否";
        String terminalDisplay = "true".equals(content.getString("terminalDisplay")) ? "是" : "否";
        String ttsPlay = "true".equals(content.getString("ttsPlay")) ? "是" : "否";
        JSONObject result = new JSONObjectPro()
            .put("urgency", urgency)
            .put("terminalDisplay", terminalDisplay)
            .put("ttsPlay", ttsPlay)
            .put("sendContent", content.getString("content"))
            .put("sendStatus", DictUtils.getDictLabel("orderSendStatus", record.getStatus()))
            .put("responseStatus", record.isSendSuccess() ? "成功" : "失败")
            .put("failReason", record.getFailReason())
            .put("responseTime", DateUtils.parseDateTime(record.getCreateTime()));
        return result;
    }
}
